import { getApiBaseUrl, getImageBaseUrl } from './env';

/**
 * 通用工具函数
 */
export const API_BASE_URL = getApiBaseUrl();
export const IMAGE_BASE_URL = getImageBaseUrl();

/**
 * 生成指定长度的随机字符串
 * @param length 字符串长度
 * @returns 随机字符串
 */
export const generateRandomString = (length: number): string => {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    const charsLength = chars.length;
    for (let i = 0; i < length; i++) {
        result += chars.charAt(Math.floor(Math.random() * charsLength));
    }
    return result;
};

/**
 * 格式化手机号码，将中间4位替换为*号
 * @param phone 手机号码
 * @returns 格式化后的手机号码
 */
export const vaildPhone = (phone: string): boolean => {
    return !/^1[0-9]\d{9}$/.test(phone);
};

/**
 * 获取中文姓名渐变颜色
 * @param type 0: 女性 1: 男性
 * @returns 渐变色数组 [开始颜色, 结束颜色]
 */
export const getChineseGradientColors = (type: number | string): string[] => {
    // 女性渐变色
    const femaleColors = [
        ['#ff6b6b', '#ff8e8e'],
        ['#ff9ff3', '#feca57'],
        ['#ff9ff3', '#5f27cd'],
        ['#ff7675', '#d63031'],
        ['#fd79a8', '#e84393'],
        ['#fdcb6e', '#f39c12'],
        ['#fd79a8', '#d63031'],
        ['#ff9ff3', '#e84393'],
        ['#fd79a8', '#fdcb6e'],
        ['#ff7675', '#fd79a8']
    ];
    
    // 男性渐变色
    const maleColors = [
        ['#5f27cd', '#54a0ff'],
        ['#54a0ff', '#2e86de'],
        ['#0abde3', '#54a0ff'],
        ['#54a0ff', '#0984e3'],
        ['#5f27cd', '#2d3436'],
        ['#2d3436', '#636e72'],
        ['#6c5ce7', '#5f27cd'],
        ['#0984e3', '#74b9ff'],
        ['#00b894', '#00cec9'],
        ['#0abde3', '#00cec9']
    ];
    
    // 选择颜色数组
    const colors = type === 0 ? femaleColors : maleColors;
    
    // 如果传入的是字符串或数字，使用它作为索引获取颜色
    if (typeof type === 'string' || typeof type === 'number') {
        const index = Number(type) % colors.length;
        return colors[index] || colors[0];
    }
    // 默认返回第一个颜色
    return colors[0];
};


export const formatDateTime = (date: Date): string => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};